package com.tuo51.manager.repository.impl;

import com.tuo51.foudation.repository.BaseNativeSqlRepository;
import com.tuo51.manager.repository.TagRelationRepository;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

@Repository
public class TagRelationRepositoryImpl extends BaseNativeSqlRepository implements TagRelationRepository {


    @Override
    public List<String> tagIdsByType(Integer type, PageRequest pageRequest) {
        String sql = "SELECT DISTINCT(`objId`)  FROM `mrel_object_tag` r" +
                ((type != null) ? (" where r.`objType` = " + type) :"") +
                " order by id DESC ";

        String limit = String.format(" LIMIT %d, %d", pageRequest.getPageNumber() * pageRequest.getPageSize(), pageRequest.getPageSize());
        sql += limit;

        List<Object[]> queryResult = sqlArrayList(sql);

        List<String> result = queryResult.stream().map(k->k[0].toString()).collect(Collectors.toList());

        return result;
    }
}
